<html>
<head><meta charset="utf-8"><title>Get address of method without naming Self type · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Get.20address.20of.20method.20without.20naming.20Self.20type.html">Get address of method without naming Self type</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="210689458"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Get%20address%20of%20method%20without%20naming%20Self%20type/near/210689458" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Diggsey <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Get.20address.20of.20method.20without.20naming.20Self.20type.html#210689458">(Sep 20 2020 at 22:57)</a>:</h4>
<p>Is there any way to get the address of an inherent method without naming the Self type explicitly? In other words, I have a variable <code>x</code> of some type T which I cannot name, and I know an inherent method called "foo" exists, callable as <code>x.foo()</code>. Can I get the address of foo?</p>



<a name="210690385"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Get%20address%20of%20method%20without%20naming%20Self%20type/near/210690385" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Get.20address.20of.20method.20without.20naming.20Self.20type.html#210690385">(Sep 20 2020 at 23:23)</a>:</h4>
<p>You could feed <code>&amp;x</code> to a helper <code>fn foo&lt;T&gt;(_: &amp;T) -&gt; ...</code></p>



<a name="210690466"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Get%20address%20of%20method%20without%20naming%20Self%20type/near/210690466" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Get.20address.20of.20method.20without.20naming.20Self.20type.html#210690466">(Sep 20 2020 at 23:25)</a>:</h4>
<p>Then that can return <code>T::foo</code> as a pointer or <code>usize</code> or whatever</p>



<a name="210690536"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Get%20address%20of%20method%20without%20naming%20Self%20type/near/210690536" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Get.20address.20of.20method.20without.20naming.20Self.20type.html#210690536">(Sep 20 2020 at 23:26)</a>:</h4>
<p>Well, ok, it's not that simple, I'm too lazy this Sunday</p>



<a name="210690554"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Get%20address%20of%20method%20without%20naming%20Self%20type/near/210690554" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Get.20address.20of.20method.20without.20naming.20Self.20type.html#210690554">(Sep 20 2020 at 23:28)</a>:</h4>
<p>I'm guessing you really have some <code>Foo&lt;X, Y, Z&gt;</code>, where one of those parameters is the unnameable bit</p>



<a name="210690601"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Get%20address%20of%20method%20without%20naming%20Self%20type/near/210690601" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Get.20address.20of.20method.20without.20naming.20Self.20type.html#210690601">(Sep 20 2020 at 23:28)</a>:</h4>
<p>So you'd parameterize that one in the helper to give it a name</p>



<a name="210691008"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Get%20address%20of%20method%20without%20naming%20Self%20type/near/210691008" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Diggsey <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Get.20address.20of.20method.20without.20naming.20Self.20type.html#210691008">(Sep 20 2020 at 23:41)</a>:</h4>
<p>unfortunately, no, the method is on a completely unknown type</p>



<a name="210691061"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Get%20address%20of%20method%20without%20naming%20Self%20type/near/210691061" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Diggsey <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Get.20address.20of.20method.20without.20naming.20Self.20type.html#210691061">(Sep 20 2020 at 23:42)</a>:</h4>
<p>I'm looking to write a macro like <code>my_macro!(x.foo())</code> which can expand to something that uses the address of the <code>T::foo</code> method</p>



<a name="210691074"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Get%20address%20of%20method%20without%20naming%20Self%20type/near/210691074" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Diggsey <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Get.20address.20of.20method.20without.20naming.20Self.20type.html#210691074">(Sep 20 2020 at 23:43)</a>:</h4>
<p>I have no idea what the type of the <code>x</code> expression is</p>



<a name="210708849"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Get%20address%20of%20method%20without%20naming%20Self%20type/near/210708849" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Get.20address.20of.20method.20without.20naming.20Self.20type.html#210708849">(Sep 21 2020 at 07:31)</a>:</h4>
<p>just in case, be aware that function addresses in Rust are "unstable" (not in a semver sense but in an LLVM sense). the only meaningful operation on them is to call them; comparing them for equality is not meaningful.</p>



<a name="210708937"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Get%20address%20of%20method%20without%20naming%20Self%20type/near/210708937" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Get.20address.20of.20method.20without.20naming.20Self.20type.html#210708937">(Sep 21 2020 at 07:32)</a>:</h4>
<p>so maybe one possible hack you could use is to use the closure <code>|x| x.foo()</code>, which captures nothing and thus is coercible to a <code>fn(_)</code></p>



<a name="210708964"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Get%20address%20of%20method%20without%20naming%20Self%20type/near/210708964" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Get.20address.20of.20method.20without.20naming.20Self.20type.html#210708964">(Sep 21 2020 at 07:33)</a>:</h4>
<p>you'll just have to nudge type inference to infer the right type for the <code>x</code> argument here...</p>



<a name="210723384"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Get%20address%20of%20method%20without%20naming%20Self%20type/near/210723384" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Diggsey <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Get.20address.20of.20method.20without.20naming.20Self.20type.html#210723384">(Sep 21 2020 at 10:21)</a>:</h4>
<p>yeah the only thing I'm going to be doing with it is calling it - the closure option is probably my best bet. The issue with that is the return type has a lifetime linked to <code>x</code>, ie. the function signature is <code>for&lt;'a&gt; fn(&amp;'a self, arg) -&gt; impl Future&lt;Output&gt; + 'a</code> and there's no way to define a closure with this signature</p>



<a name="210723594"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Get%20address%20of%20method%20without%20naming%20Self%20type/near/210723594" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Diggsey <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Get.20address.20of.20method.20without.20naming.20Self.20type.html#210723594">(Sep 21 2020 at 10:24)</a>:</h4>
<p>I could maybe use some unsafe code to temporarily cast away the lifetimes</p>



<a name="210767834"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Get%20address%20of%20method%20without%20naming%20Self%20type/near/210767834" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Get.20address.20of.20method.20without.20naming.20Self.20type.html#210767834">(Sep 21 2020 at 16:45)</a>:</h4>
<p>you can nudge input type inference like so:</p>
<div class="codehilite"><pre><span></span><code><span class="kd">let</span><span class="w"> </span><span class="n">f</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">|</span><span class="n">x</span><span class="o">|</span><span class="w"> </span><span class="n">x</span><span class="p">.</span><span class="n">foo</span><span class="p">();</span><span class="w"></span>
<span class="k">if</span><span class="w"> </span><span class="kc">false</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="nb">drop</span><span class="p">(</span><span class="n">f</span><span class="p">(</span><span class="n">x</span><span class="p">));</span><span class="w"> </span><span class="n">unreachable</span><span class="o">!</span><span class="p">()</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="210767844"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Get%20address%20of%20method%20without%20naming%20Self%20type/near/210767844" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Get.20address.20of.20method.20without.20naming.20Self.20type.html#210767844">(Sep 21 2020 at 16:45)</a>:</h4>
<p>can't the return type be inferred?</p>



<a name="210778214"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Get%20address%20of%20method%20without%20naming%20Self%20type/near/210778214" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Diggsey <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Get.20address.20of.20method.20without.20naming.20Self.20type.html#210778214">(Sep 21 2020 at 18:04)</a>:</h4>
<p>Closures simply don't support this kind of generic return type (where it's a type implementing Future but parameterized by the lifetime of the closure argument) AFAIK</p>



<a name="210778263"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Get%20address%20of%20method%20without%20naming%20Self%20type/near/210778263" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Diggsey <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Get.20address.20of.20method.20without.20naming.20Self.20type.html#210778263">(Sep 21 2020 at 18:05)</a>:</h4>
<p>I think this is the same reason why async closures are not a thing</p>



<a name="210779921"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Get%20address%20of%20method%20without%20naming%20Self%20type/near/210779921" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Get.20address.20of.20method.20without.20naming.20Self.20type.html#210779921">(Sep 21 2020 at 18:17)</a>:</h4>
<p><span class="user-mention silent" data-user-id="295632">Diggory Blake</span> <a href="#narrow/stream/122651-general/topic/Get.20address.20of.20method.20without.20naming.20Self.20type/near/210778263">said</a>:</p>
<blockquote>
<p>I think this is the same reason why async closures are not a thing</p>
</blockquote>
<p>You mean not yet stabilized?</p>



<a name="210781975"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Get%20address%20of%20method%20without%20naming%20Self%20type/near/210781975" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Diggsey <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Get.20address.20of.20method.20without.20naming.20Self.20type.html#210781975">(Sep 21 2020 at 18:35)</a>:</h4>
<p>I mean that they don't work in the way async closures should work:<br>
<a href="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=a0704e23c1b15683cf893a901238509e">https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=a0704e23c1b15683cf893a901238509e</a></p>



<a name="210782287"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Get%20address%20of%20method%20without%20naming%20Self%20type/near/210782287" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Diggsey <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Get.20address.20of.20method.20without.20naming.20Self.20type.html#210782287">(Sep 21 2020 at 18:37)</a>:</h4>
<p>there's no way to tie the lifetime of the future returned by the closure to the lifetime of the closure's first argument</p>



<a name="210787465"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Get%20address%20of%20method%20without%20naming%20Self%20type/near/210787465" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Get.20address.20of.20method.20without.20naming.20Self.20type.html#210787465">(Sep 21 2020 at 19:18)</a>:</h4>
<p>FWIW, I've been unable to express that the returned future was tied to the higher-order lifetime of the input closure: <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=1144e195f6703152873288bb8a3a208c">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=1144e195f6703152873288bb8a3a208c</a><br>
In the unlikely event you do not need a higher-order lifetime and can use one fixed capturing that of the value fed to your macro, then you could do: <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=56657c5bc69860500b44f16dbf557525">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=56657c5bc69860500b44f16dbf557525</a></p>
<p>EDIT: I've managed to achieve it but by boxing the returned future, since otherwise we stumble upon Rust limitation when dealing with higher-order lifetimes and resolving type equality: <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=a09d9b498ea57271fe1fe4922feaf743">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=a09d9b498ea57271fe1fe4922feaf743</a></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>